package com.caregiver.watch.business.care.model.vo;

import com.alibaba.fastjson2.JSON;
import com.caregiver.watch.business.care.model.bo.AlertMessageBo;
import com.caregiver.watch.business.care.model.po.UserAlertRecord;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: 预警历史VO
 * @Author 疆戟
 * @Date 2025/10/15 23:55
 * @Version 1.0
 */
@Data
@Schema(description = "预警历史VO")
public class WarningHistoryVo {

    /**
     * 预警标题
     */
    @Schema(description = "预警标题")
    private String warningTitle;

    @Schema(description = "预警时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime warningTime;

    @Schema(description = "预警类型")
    private String warningType;

    @Schema(description = "预警值")
    private String warningValue;

    @Schema(description = "警戒值")
    private String threshold;

    @Schema(description = "被照护人ID")
    private Long recipientId;

    @Schema(description = "被照护人姓名")
    private String recipientName;

    /**
     * 被照护人头像
     */
    @Schema(description = "被照护人头像")
    private String recipientAvatar;

    // 处理建议
    @Schema(description = "处理建议")
    private List<String> handleSuggest;

    public static WarningHistoryVo build(UserAlertRecord data) {
        WarningHistoryVo vo = new WarningHistoryVo();
        vo.setWarningTime(data.getAlertTime());
        if (StringUtils.isNotBlank(data.getAlertMessage())) {
            AlertMessageBo alertMessageBo = JSON.parseObject(data.getAlertMessage(), AlertMessageBo.class);
            vo.setWarningTitle(alertMessageBo.getWarningTitle());
            vo.setWarningValue(alertMessageBo.getWarningValue());
            vo.setThreshold(alertMessageBo.getThreshold());
            vo.setHandleSuggest(alertMessageBo.getWarningContent());
        }
        vo.setWarningType(data.getAlertType());
        vo.setRecipientId(data.getRecipientId());
        return vo;
    }
}
